package edu.ncsu.contractminer.event.categorization;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import edu.ncsu.contractminer.event.StringToNumericForWeka;
import edu.ncsu.contractminer.utilities.Constants;
import weka.classifiers.Classifier;
import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;
import weka.experiment.InstanceQuery;

import weka.core.converters.ConverterUtils.DataSource;

public class EventClassifier {
	public static void main(String[] args) {
		try {
			Classifier classifier = (Classifier) weka.core.SerializationHelper
					.read(Constants.EventClassificationModelFile);

			DataSource source = new DataSource(Constants.wekaFile);
			Instances dataset = source.getDataSet();
			dataset.setClassIndex(dataset.numAttributes() - 1);

			Instance instance = new Instance(dataset.numAttributes());
			instance.setDataset(dataset);

			System.out.println((true == false) ? 1 : 0);

			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			String dataSourceName = "NormDB";
			String dbURL = "jdbc:odbc:" + dataSourceName;
			Connection con = DriverManager.getConnection(dbURL, "", "");
			// try and create a java.sql.Statement so we can run queries
			Statement statement = con.createStatement();

			String query0 = "SELECT ID, Subject, EventCandidate, SubContainsNE, ClauseSignal, ModalVerb, CounterClauseSignal FROM "
					+ Constants.eventTableName;
			statement.execute(query0);
			ResultSet result0 = statement.getResultSet();
			if (result0 != null) {
				while (result0.next()) {

					Long id = result0.getLong("ID");
					String subject = result0.getString("Subject");
					String eventCandidate = result0.getString("EventCandidate");
					String modalVerb = result0.getString("ModalVerb");
					String ClauseSignal = result0.getString("ClauseSignal");
					String CounterClauseSignal = result0
							.getString("CounterClauseSignal");
					boolean SubContainsNE = result0.getBoolean("SubContainsNE");

					instance.setValue(dataset.attribute(0),
							StringToNumericForWeka.booleanToInt(SubContainsNE));

					instance.setValue(dataset.attribute(1),
							StringToNumericForWeka
									.modalVerbStringToNumerical(modalVerb));
					instance
							.setValue(
									dataset.attribute(2),
									StringToNumericForWeka
											.subClauseWordStringToNumerical(ClauseSignal));
					instance
							.setValue(
									dataset.attribute(3),
									StringToNumericForWeka
											.subClauseWordStringToNumerical(CounterClauseSignal));

					System.out.println(subject + " " + eventCandidate);
					System.out.println(" Features: " + instance);
					System.out.println(" Class: "
							+ classifier.classifyInstance(instance));
					double c = classifier.classifyInstance(instance);
					String isEvent = "false";
					if (c == 1)
						isEvent = "true";
					String query1 = "UPDATE " + Constants.eventTableName
							+ " SET IsEvent=" + isEvent + " WHERE ID=" + id;
					System.out.println(query1);
					Statement statement2 = con.createStatement();
					statement2.executeUpdate(query1);

				}
			}

			/*
			 * InstanceQuery query = new InstanceQuery(); query.setUsername("");
			 * query.setPassword(""); query.setQuery("select id from Student2");
			 * Instances data = query.retrieveInstances();
			 */

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
